package com.example.androidshixi;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bottomNavigationView = findViewById(R.id.bottom_navigation);

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull android.view.MenuItem item) {
                Fragment selectedFragment = null;
                int itemId = item.getItemId();
                if (itemId == R.id.headlines_item) {
                    selectedFragment = new HeadlinesFragment();
                } else if (itemId == R.id.mall_item) {
                    selectedFragment = new MallFragment();
                } else if (itemId == R.id.my_item) {
                    selectedFragment = new MyFragment();
                }
                if (selectedFragment != null) {
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.fragment_container, selectedFragment);
                    transaction.commit();
                }
                return true;
            }
        });

        // 检查是否携带了显示商城界面的标识
        boolean showMall = getIntent().getBooleanExtra("show_mall", false);
        if (showMall) {
            // 显示商城界面
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.fragment_container, new MallFragment());
            transaction.commit();
            // 设置底部导航栏选中商城项
            bottomNavigationView.setSelectedItemId(R.id.mall_item);
        } else {
            // 默认显示头条页面
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.fragment_container, new HeadlinesFragment());
            transaction.commit();
        }
    }
}